<?php


namespace servers\cyf;


use servers\CurlServer;

class FaceServer
{
    /**
     * post
     * @param string $url
     * @param string $param
     * @return bool|mixed
     */
    function request_post($url = '', $param = '')
    {
        if (empty($url) || empty($param)) {
            return false;
        }

        $postUrl = $url;
        $curlPost = $param;
        // 初始化curl
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $postUrl);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        // post提交方式
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        // 运行curl
        $data = curl_exec($curl);
        curl_close($curl);

        return json_decode($data);
    }


    /**
     * 获取token
     * @return bool|string
     * @cyf
     */
    public function getToken()
    {
        $client_id = '5afwGAz7yb2o1In6S9IvsGsV';
        $client_secret = 'f8a7hZSUGN9FYfDYI4o8DWYgROQb29sZ';

        $url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=$client_id&client_secret=$client_secret&";

        $curl = new CurlServer();
        $data = $curl->post($url);
//        var_dump(json_decode($data));die();
        return json_decode($data,true)['access_token'];
    }

    /**
     * 人脸检测
     * @cyf
     */
    public function facedete($image,$image_type='')
    {

        $token =$this->getToken();
        $url = 'https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=' . $token;
        $bodys =  "{\"image\":\"$image\",\"image_type\":\"$image_type\",\"face_field\":\"faceshape,facetype\"}";
        $res = $this->request_post($url, $bodys);

        return json_decode(json_encode($res),true);
    }


    /**
     * 添加用户组
     * @param $group
     * @return bool
     * @throws \Exception
     * @cyf
     */
    public function addGrooup($group)
    {
        $token = $this->getToken();
        $url = 'https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/add?access_token=' . $token;
        $bodys = "{\"group_id\":\"$group\"}";
        $res = $this->request_post($url, $bodys);
        if($res->error_code != 0) throw new \Exception('添加用户组失败');
        return true;
    }

    /**
     * 删除用户组
     * @param $group
     * @return bool
     * @throws \Exception
     * @cyf
     */
    public function delGroup($group)
    {
        $token = $this->getToken();
        $url = 'https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/delete?access_token=' . $token;
        $bodys = "{\"group_id\":\"$group\"}";
        $res = $this->request_post($url, $bodys);
        if($res->error_code != 0) throw new \Exception('添加用户组失败');
        return true;
    }

    /**
     * 人脸添加
     * @param $image
     * @param $image_type
     * @param $group_id
     * @param $user_id
     * @cyf
     */
    public function userAdd($image,$image_type,$group_id,$user_id)
    {
        $token = $this->getToken();
        $url = 'https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add?access_token=' . $token;
        $bodys = "{\"image\":\"$image\",\"image_type\":\"$image_type\",\"group_id\":\"$group_id\",\"user_id\":\"$user_id\"}";
        $res = $this->request_post($url, $bodys);
        if($res->error_code != 0) throw new \Exception('添加人脸失败');
        return true;
    }

    /**
     * 人脸搜索
     * @param $image
     * @param $image_type
     * @param $group_id
     * @cyf
     */
    public function fachSeach($image,$image_type,$group_id='')
    {
        $token = $this->getToken();
        $url = 'https://aip.baidubce.com/rest/2.0/face/v3/search?access_token=' . $token;
        $bodys = "{\"image\":\"$image\",\"image_type\":\"$image_type\",\"group_id_list\":\"cyf2008a\",\"quality_control\":\"LOW\",\"liveness_control\":\"NONE\"}";

        $res = $this->request_post($url, $bodys);
        if($res->error_code != 0) throw new \Exception('人脸不存在');
        return true;
    }


}